package com.betterjr.modules.accountSupervision.service;

import com.betterjr.common.service.BaseService;
import com.betterjr.mapper.pagehelper.Page;
import com.betterjr.modules.accountSupervision.dao.ScfAccountDetailMapper;
import com.betterjr.modules.accountSupervision.entity.ScfAccountDetailBean;
import com.betterjr.modules.accountSupervision.utils.ScfRomoteUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 账户查询
 */
@Service
public class ScfAccountDetailService extends BaseService<ScfAccountDetailMapper, ScfAccountDetailBean> {
    public static String CONSTANT_QUERY_DTL_WSDL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><header><reqTime></reqTime><tradeCode></tradeCode><serialId></serialId></header><body><entity><accountCode></accountCode></entity><list></list></body></request>";

    /**
     * 余额查询
     * @param anMap
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public Page<ScfAccountDetailBean> queryAccounBalanceList(Map<String, Object> anMap, final int anPageNum, final int anPageSize) {
        if(anMap.size() < 3){
            return  new Page<>();
        }
        Map<String,Object> map = this.mapper.getCount(anMap);
        Long count = (Long) map.get("num");
        int start = (anPageNum-1)*anPageSize;
        anMap.put("start",start);
        anMap.put("anPageSize",anPageSize);
        List<ScfAccountDetailBean> list = new ArrayList<>();
        list = this.mapper.getList(anMap);
        for (ScfAccountDetailBean bean : list){
            // 通过接口获取值
            final Map<String, Object> datas = new HashMap<>();
            Map<String, Object> result = new HashMap<>();
            datas.put("accountCode",bean.getAccountNo());
            datas.put("supplierId",bean.getSupplierId());
            result = ScfRomoteUtil.callService("QueryCurBalCpf", datas, ScfRomoteUtil.CONSTANT_QUERY_BALANCE_WSDL);
            BigDecimal bal = BigDecimal.ZERO;
            BigDecimal availBal = BigDecimal.ZERO;
            if(result.get("data") != null){
                List<Map<String, String>>  mapList = (List<Map<String, String>>)result.get("data");
                if(null != mapList.get(0).get("remainMoney") && !"".equals(mapList.get(0).get("remainMoney")))
                    bal = new BigDecimal(mapList.get(0).get("remainMoney"));
                if(null != mapList.get(0).get("usableMoney") && !"".equals(mapList.get(0).get("usableMoney")))
                    availBal = new BigDecimal(mapList.get(0).get("usableMoney"));
            }
            bean.setBalance(bal);
            bean.setAvailableBalance(availBal);
        }
        Page<ScfAccountDetailBean> page = Page.listToPage(list);
        page.setPageNum(anPageNum);
        page.setPageSize(anPageSize);
        page.setTotal(count);
        return page;
    }

    /**
     * 统计查询
     * @param anMap
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public Page<ScfAccountDetailBean> queryAccounCountList(Map<String, Object> anMap, final int anPageNum, final int anPageSize) {
        if(anMap.size() < 3){
            return  new Page<>();
        }
        Map<String,Object> map = this.mapper.getCount(anMap);
        Long count = (Long) map.get("num");
        int start = (anPageNum-1)*anPageSize;
        anMap.put("start",start);
        anMap.put("anPageSize",anPageSize);
        List<ScfAccountDetailBean> list = new ArrayList<>();
        list = this.mapper.getList(anMap);
        for (ScfAccountDetailBean bean : list){
            // 通过接口获取值
            final Map<String, Object> datas = new HashMap<>();
            Map<String, Object> result = new HashMap<>();
            datas.put("accountCode",bean.getAccountNo());
            datas.put("supplierId",bean.getSupplierId());
            result = ScfRomoteUtil.callService("QueryCurBalCpf", datas, ScfRomoteUtil.CONSTANT_QUERY_BALANCE_WSDL);
            BigDecimal bal = BigDecimal.ZERO;
            BigDecimal availBal = BigDecimal.ZERO;
            if(result.get("data") != null){
                List<Map<String, String>>  mapList = (List<Map<String, String>>)result.get("data");
                if(null != mapList.get(0).get("remainMoney") && !"".equals(mapList.get(0).get("remainMoney")))
                    bal = new BigDecimal(mapList.get(0).get("remainMoney"));
                if(null != mapList.get(0).get("usableMoney") && !"".equals(mapList.get(0).get("usableMoney")))
                    availBal = new BigDecimal(mapList.get(0).get("usableMoney"));
            }
            bean.setBalance(bal);
            bean.setAvailableBalance(availBal);
            bean.setMonth(new Date().getMonth()+1+"");
            datas.put("tradeDate",bean.getMonth());
            result = ScfRomoteUtil.callService("QueryCurDtlCpf", datas, ScfRomoteUtil.CONSTANT_QUERY_DTL_MONTH_WSDL);
            BigDecimal monthPay = BigDecimal.ZERO;
            BigDecimal monthIncome = BigDecimal.ZERO;
            if(result.get("data") != null){
                List<Map<String, String>>  mapList = (List<Map<String, String>>)result.get("data");
                if(null != mapList.get(0).get("amountPaid") && !"".equals(mapList.get(0).get("amountPaid")))
                    monthPay = new BigDecimal(mapList.get(0).get("amountPaid"));
                if(null != mapList.get(0).get("amountReceived") && !"".equals(mapList.get(0).get("amountReceived")))
                    monthIncome = new BigDecimal(mapList.get(0).get("amountReceived"));
            }
            bean.setMonthPay(monthPay);
            bean.setMonthIncome(monthIncome);
        }
        Page<ScfAccountDetailBean> page = Page.listToPage(list);
        page.setPageNum(anPageNum);
        page.setPageSize(anPageSize);
        page.setTotal(count);
        return page;
    }
    /**
     * 流水查询
     * @param anMap
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public Page<ScfAccountDetailBean> queryAccounStreamList(Map<String, Object> anMap, final int anPageNum, final int anPageSize) {
        if(anMap.size() < 3){
            return  new Page<>();
        }
//        Map<String,Object> map = this.mapper.getCount(anMap);
//        Long count = (Long) map.get("num");
//        int start = (anPageNum-1)*anPageSize;
//        anMap.put("start",start);
//        anMap.put("anPageSize",anPageSize);
        List<ScfAccountDetailBean> list = new ArrayList<>();
        list = this.mapper.getList(anMap);
        Calendar calendar = Calendar.getInstance();
        Date newDate = new Date(System.currentTimeMillis());
        calendar.setTime(newDate);
        calendar.add(Calendar.MONTH, -1);
        Date t = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = formatter.format(t);
        String endDate = formatter.format(newDate);
        List<ScfAccountDetailBean> tempList = new ArrayList<>();
        List<ScfAccountDetailBean> pageList = new ArrayList<>();
        for (ScfAccountDetailBean bean : list){
            // 通过接口获取值
            final Map<String, Object> datas = new HashMap<>();
            Map<String, Object> result = new HashMap<>();
            datas.put("accountCode",bean.getAccountNo());
            datas.put("supplierId",bean.getSupplierId());
            datas.put("startDate",startDate);
            datas.put("endDate",endDate);
            result = ScfRomoteUtil.callService("QueryHisDtlCpf", datas, ScfRomoteUtil.CONSTANT_QUERY_DTL_WSDL);
            BigDecimal bal = BigDecimal.ZERO;
            BigDecimal tradeMoney = BigDecimal.ZERO;
            if(result != null && result.get("data") != null){
                List<Map<String, String>>  mapList = (List<Map<String, String>>)result.get("data");
                for(int i =0;i< mapList.size();i++) {
                    if (null != mapList.get(i).get("remainMoney") && !"".equals(mapList.get(i).get("remainMoney")))
                        bal = new BigDecimal(mapList.get(i).get("remainMoney"));
                    if (null != mapList.get(i).get("tradeMoney") && !"".equals(mapList.get(i).get("tradeMoney")))
                        tradeMoney = new BigDecimal(mapList.get(i).get("tradeMoney"));
                    bean.setOtherAccount(mapList.get(i).get("otherAccount"));
                    bean.setOtherAccountName(mapList.get(i).get("otherName"));
                    bean.setTransDate(mapList.get(i).get("tradeTime"));
                    bean.setBalance(bal);
                    bean.setAvailableBalance(bal);
                    bean.setTransMoney(tradeMoney);
                    bean.setAbs(mapList.get(i).get("tradeSummary"));
                    tempList.add(bean);
                }
            }

        }

        /**
         * 分页处理
         */
        Page<ScfAccountDetailBean> page = new Page<>();
        int begin = (anPageNum-1)*anPageSize;
        int end = anPageNum * anPageSize +1;
        if(end > tempList.size()){
            end = tempList.size();
        }
        for (int i=begin; i < end;i++){
            pageList.add(tempList.get(i));
        }
        page = Page.listToPage(pageList);
        page.setPageNum(anPageNum);
        page.setPageSize(anPageSize);
        page.setTotal(tempList.size());
        return page;
    }
}
